﻿namespace com.stifel.BranchMgmt.WebParts.Search
{
    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    using com.stifel.BranchMgmtDao;
    using com.stifel.Extensions.ObjectExtensions;
    using com.stifel.Extensions.StringExtensions;

    public partial class SearchUserControl : UserControl
    {
        #region Methods

        protected void Page_Load( object sender, EventArgs e )
        {
            if ( !IsPostBack )
            {
                setDefaultView( );
                hideRepeaterAndErrorMsg( );
            }
        }

        protected void uiBranchId_Activate( object sender, EventArgs e )
        {
            uiTbBranchId.Focus( );
        }

        protected void uiBranchId_Deactivate( object sender, EventArgs e )
        {
        }

        protected void uiBranchUnit_Activate( object sender, EventArgs e )
        {
            uiTbBranchUnit.Focus( );
        }

        protected void uiBranchUnit_Deactivate( object sender, EventArgs e )
        {
        }

        protected void uiBroadcastIp_Activate( object sender, EventArgs e )
        {
            uiTbBroadCastIp.Focus( );
        }

        protected void uiBroadcastIp_Deactivate( object sender, EventArgs e )
        {
        }

        /// <summary>
        /// Initiates the search
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void uiBtnSearch_Click( object sender, EventArgs e )
        {
            List<StifelBranchInfo> l = new List<StifelBranchInfo>( );
            List<BranchInfoProperties> bipList = new List<BranchInfoProperties>( );
            List<ResultsInfo> rInfoList = new List<ResultsInfo>( );
            ResultInfoCollection rinfoCollection = new ResultInfoCollection( );

            // from the active view get the programmatic identifier and compare with the command arguments of each navigation button to determine the search method requested
            View v = uiView.GetActiveView( );

            if ( uiBtnBranchId.CommandArgument.Equals( v.ID ) )
            {
                string branchId = uiTbBranchId.Text.Trim( );
                l = getBranchsByBranchId( branchId );
                if ( ( l != null ) && ( l.Count != 0 ) )
                {
                    hideMsgShowResults( );
                    bipList = getBranchInfoProperties( l );
                    rInfoList = getResultsForBinding( bipList );
                    loadResults( rInfoList );
                }
                else
                {
                    hideResultsShowMsg( );
                }
            }
            else if ( uiBtnBranchUnit.CommandArgument.Equals( v.ID ) )
            {
                string branchUnit = uiTbBranchUnit.Text.Trim( );
                l = getBranchsByBranchUnit( branchUnit );
                if ( ( l != null ) && ( l.Count != 0 ) )
                {
                    hideMsgShowResults( );
                    bipList = getBranchInfoProperties( l );
                    rInfoList = getResultsForBinding( bipList );
                    loadResults( rInfoList );
                }
                else
                {
                    hideResultsShowMsg( );
                }
            }
            else if ( uiBtnBroadcastIp.CommandArgument.Equals( v.ID ) )
            {
                string broadCastIp = uiTbBroadCastIp.Text.Trim( );
                l = getBranchsByBroadcastIp( broadCastIp );
                if ( ( l != null ) && ( l.Count != 0 ) )
                {
                    hideMsgShowResults( );
                    bipList = getBranchInfoProperties( l );
                    rInfoList = getResultsForBinding( bipList );
                    loadResults( rInfoList );
                }
                else
                {
                    hideResultsShowMsg( );
                }
            }
            else if ( uiBtnCheckCode.CommandArgument.Equals( v.ID ) )
            {
                string checkCode = uiTbCheckCode.Text.Trim( );
                l = getBranchsByCheckCode( checkCode );
                if ( ( l != null ) && ( l.Count != 0 ) )
                {
                    hideMsgShowResults( );
                    bipList = getBranchInfoProperties( l );
                    rInfoList = getResultsForBinding( bipList );
                    loadResults( rInfoList );
                }
                else
                {
                    hideResultsShowMsg( );
                }
            }
            else if ( uiBtnDestCode.CommandArgument.Equals( v.ID ) )
            {
                string destCode = uiTbDestinationCode.Text.Trim( );
                l = getBranchsByDestCode( destCode );
                if ( ( l != null ) && ( l.Count != 0 ) )
                {
                    hideMsgShowResults( );
                    bipList = getBranchInfoProperties( l );
                    rInfoList = getResultsForBinding( bipList );
                    loadResults( rInfoList );
                }
                else
                {
                    hideResultsShowMsg( );
                }
            }
            else if ( uiBtnOrg.CommandArgument.Equals( v.ID ) )
            {
                string orgCode = uiTbOrg.Text.Trim( );
                l = getBranchsByOrg( orgCode );
                if ( ( l != null ) && ( l.Count != 0 ) )
                {
                    hideMsgShowResults( );
                    bipList = getBranchInfoProperties( l );
                    rInfoList = getResultsForBinding( bipList );
                    loadResults( rInfoList );
                }
                else
                {
                    hideResultsShowMsg( );
                }
            }
            else if ( uiBtnPriceServer.CommandArgument.Equals( v.ID ) )
            {
                string priceServer = uiTbPriceServer.Text.Trim( );
                l = getBranchsByPriceServer( priceServer );
                if ( ( l != null ) && ( l.Count != 0 ) )
                {
                    hideMsgShowResults( );
                    bipList = getBranchInfoProperties( l );
                    rInfoList = getResultsForBinding( bipList );
                    loadResults( rInfoList );
                }
                else
                {
                    hideResultsShowMsg( );
                }
            }
            else if ( uiBtnSiteId.CommandArgument.Equals( v.ID ) )
            {
                string siteId = uiTbSiteId.Text.Trim( );
                l = getBranchsBySiteId( siteId );
                if ( ( l != null ) && ( l.Count != 0 ) )
                {
                    hideMsgShowResults( );
                    bipList = getBranchInfoProperties( l );
                    rInfoList = getResultsForBinding( bipList );
                    loadResults( rInfoList );
                }
                else
                {
                    hideResultsShowMsg( );
                }
            }
            else if ( uiBtnSpeedDial.CommandArgument.Equals( v.ID ) )
            {
                string speedDial = uiTbSpeedDial.Text.Trim( );
                l = getBranchsBySpeedDial( speedDial );
                if ( ( l != null ) && ( l.Count != 0 ) )
                {
                    hideMsgShowResults( );
                    bipList = getBranchInfoProperties( l );
                    rInfoList = getResultsForBinding( bipList );
                    loadResults( rInfoList );
                }
                else
                {
                    hideResultsShowMsg( );
                }
            }
            else if ( uiBtnThompsonId.CommandArgument.Equals( v.ID ) )
            {
                string thompsonId = uiTbThomsonId.Text.Trim( );
                l = getBranchsByThompsonId( thompsonId );
                if ( ( l != null ) && ( l.Count != 0 ) )
                {
                    hideMsgShowResults( );
                    bipList = getBranchInfoProperties( l );
                    rInfoList = getResultsForBinding( bipList );
                    loadResults( rInfoList );
                }
                else
                {
                    hideResultsShowMsg( );
                }
            }
            else if ( uiBtnWireCode.CommandArgument.Equals( v.ID ) )
            {
                string wireCode = uiTbWireCode.Text.Trim( );
                l = getBranchsByWireCode( wireCode );
                if ( ( l != null ) && ( l.Count != 0 ) )
                {
                    hideMsgShowResults( );
                    bipList = getBranchInfoProperties( l );
                    rInfoList = getResultsForBinding( bipList );
                    loadResults( rInfoList );
                }
                else
                {
                    hideResultsShowMsg( );
                }
            }
            rinfoCollection.resultInfoList = rInfoList;
            uiHiddenResultValue.Value = rinfoCollection.SerializeToXml<ResultInfoCollection>( ).ToBase64String( );
            //hideOrUnHideRepeater( );
        }

        protected void uiCheckCode_Activate( object sender, EventArgs e )
        {
            uiTbCheckCode.Focus( );
        }

        protected void uiCheckCode_Deactivate( object sender, EventArgs e )
        {
        }

        protected void uiDestCode_Activate( object sender, EventArgs e )
        {
            uiTbDestinationCode.Focus( );
        }

        protected void uiDestCode_Deactivate( object sender, EventArgs e )
        {
        }

        protected void uiNavigationTab_Click( object sender, EventArgs e )
        {
            ResultInfoCollection rInfoCollection = uiHiddenResultValue.Value.FromBase64String( ).DeserializeFromXml<ResultInfoCollection>( );
            Button button = sender as Button;
            navigateToSelectedView( button );
            if ( rInfoCollection == null )
                loadResults( null );
            else
                loadResults( rInfoCollection.resultInfoList );
        }

        protected void uiOrg_Activate( object sender, EventArgs e )
        {
            uiTbOrg.Focus( );
        }

        protected void uiOrg_Deactivate( object sender, EventArgs e )
        {
        }

        protected void uiPriceServer_Activate( object sender, EventArgs e )
        {
            uiTbPriceServer.Focus( );
        }

        protected void uiPriceServer_Deactivate( object sender, EventArgs e )
        {
        }

        //protected void uiResults_ItemCommand( object source, RepeaterCommandEventArgs e )
        //{
        //    if ( e.CommandName.Equals( "Go", StringComparison.InvariantCultureIgnoreCase ) )
        //    {
        //        HiddenField hiddenValue = ( HiddenField ) e.Item.FindControl( "uiBranch_Guid" );
        //        Guid ItemGuid = hiddenValue.Value.ToGuid( );
        //        if ( !string.IsNullOrEmpty( TargetPage ) )
        //        {
        //            Button B = ( Button ) e.Item.FindControl( "uiBtnGo" );
        //            Page.Response.Redirect( TargetPage.Append( "?ItemGuid=" + ItemGuid.ToString( ) ) );
        //        }
        //        else
        //        {
        //        }
        //    }
        //}
        protected void uiResults_ItemDataBound( object sender, RepeaterItemEventArgs e )
        {
            if ( ( e.Item.ItemType == ListItemType.Item ) || ( e.Item.ItemType == ListItemType.AlternatingItem ) )
            {
                PlaceHolder uiPlaceHolderBranchBasicInfo = ( PlaceHolder ) e.Item.FindControl( "uiPhBranchBasicInfo" );
                PlaceHolder uiPlaceHolderUnit = ( PlaceHolder ) e.Item.FindControl( "uiPhUnit" );
                Label uiPromortionMsg = ( Label ) e.Item.FindControl( "uiLblPromotingMsg" );
                Literal uiGo = ( Literal ) e.Item.FindControl( "uiLitGo" );
                HiddenField uiCompleteInfo = ( HiddenField ) e.Item.FindControl( "uiBranchInfo_Value" );
                BranchInfoProperties bip = uiCompleteInfo.Value.FromBase64String( ).DeserializeFromXml<BranchInfoProperties>( );
                if ( isNotPromoting( bip.ItemGuid ) == true )
                {
                    uiGo.Visible = true;
                    uiPromortionMsg.Visible = false;
                }
                else
                {
                    uiGo.Visible = false;
                    uiPromortionMsg.Visible = true;
                }
                setBranchBasicInfo( uiPlaceHolderBranchBasicInfo, bip );
                setBranchUnitInfo( uiPlaceHolderUnit, bip );
            }
        }

        protected void uiSiteId_Activate( object sender, EventArgs e )
        {
            uiTbSiteId.Focus( );
        }

        protected void uiSiteId_Deactivate( object sender, EventArgs e )
        {
        }

        protected void uiSpeedDial_Activate( object sender, EventArgs e )
        {
            uiTbSpeedDial.Focus( );
        }

        protected void uiSpeedDial_Deactivate( object sender, EventArgs e )
        {
        }

        protected void uiThompsonId_Activate( object sender, EventArgs e )
        {
            uiTbThomsonId.Focus( );
        }

        protected void uiThompsonId_Deactivate( object sender, EventArgs e )
        {
        }

        protected void uiWireCode_Activate( object sender, EventArgs e )
        {
            uiTbWireCode.Focus( );
        }

        protected void uiWireCode_Deactivate( object sender, EventArgs e )
        {
        }

        void loadResults( List<ResultsInfo> values )
        {
            if ( values == null || values.Count <= 0 )
                return;

            //uiResults.Visible = true;
            //uiSearch.Visible = false;
            uiResults_Repeater.DataSource = values;
            uiResults_Repeater.DataBind( );
        }

        #endregion Methods
    }
}